/*
 *  lexer.cpp
 *  framework
 *
 *  Created by gerhard gubler on 15.11.09.
 *  Copyright 2009 md80.ch . All rights reserved.
 *
 */

#include "lexer.h"
#include <iostream>

using std::string;
using std::cout;
using std::endl;

lexer::lexer()
{
	multi_line_comment_begin_set = false;
	multi_line_comment_end_set = false;

}

void lexer::lex(string to_lex)
{
	be_lexed = to_lex;
	current = be_lexed.begin();
	last = be_lexed.end();
	the_end = false;
	special_delim = false;
	token = false;
}


string lexer::operator*() const
{
	return word;
}


void lexer::operator++()
{
	word = "";
	if (special_delim == true)
	{
		word = *current;
		special_delim = false;
		*current++;
		return;
	}
	while (current != last)
	{
		if (multi_line_comment_end_set == true and multi_line_comment_begin_set == true and (*current == multi_line_comment_begin.at(0) and *current + 1 == multi_line_comment_begin.at(1)))			
		{
			cout << "FOUND MULTI LINE COMMENT BEGIN " << endl;
			
		}	
		if (iscomment(*current))
		{
			while (*current != '\n')
			{
				if (current == last)
				{
					the_end = true;
					break;
				}
				*current++;
			}
		}
		if (isdelemiter(*current))
		{
			special_delim = true;
			return;
		}
		if (*current == ' ' or *current == 0)
		{
			while( *current == ' ')
			{
				*current++;
			}
			if (token == true)
			{
				token = false;				
				return;
			}
		}
		word += *current;
		*current++;
		token = true;
	}
	if (current == last)
	{
		the_end = true;
	}	
	*current++;
}

void lexer::setdelimiter(string delim)
{
	specialdelemiter = delim;	
}

bool lexer::isdelemiter(char chr)
{
	return specialdelemiter.find(chr) != std::string::npos;
}

void lexer::setcommentchars(string comchr)
{
   	commentchars = comchr;
}

bool lexer::iscomment(char chr)
{
	return commentchars.find(chr) != std::string::npos;
}

void lexer::setMultiLineCommentBegin(std::string lrval)
{
	multi_line_comment_begin = lrval;
	multi_line_comment_begin_set = true;
}

void lexer::setMultiLineCommentEnd(std::string lrval)
{
	multi_line_comment_end = lrval;
	multi_line_comment_end_set = true;
}
